package com.zdkj.subway.stm.handler;
/**
 * @author 陈博
 * @date 2020/1/18 0018 14:36
 */

import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.dto.in.SwStOperateLogDTO;
import com.zdkj.subway.stm.enums.LogTypeEnum;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @program: zdkj
 * @description: 日志添加处理handler
 * @author: chen
 * @create: 2020-01-18 14:36
 **/
public class LogHandler {
    /**
     * 修改日志list
     *
     * @param
     * @param
     * @return
     */
    public static List<SwStOperateLogDTO> setLogParms(Map<String, Map> fileds, Object o) {
        Iterator<Map.Entry<String, Map>> iterator = fileds.entrySet().iterator();

        ArrayList<SwStOperateLogDTO> list = new ArrayList<>();
        while (iterator.hasNext()) {
            SwStOperateLogDTO swStOperateLogDTO = new SwStOperateLogDTO();
            Map.Entry<String, Map> next = iterator.next();
            String filedName = next.getKey();
            Map value = next.getValue();
            String Before = null == value.get("objBefore") ? "" : value.get("objBefore").toString();
            String After = null == value.get("objAfter") ? "" : value.get("objAfter").toString();
            swStOperateLogDTO.setLogType(LogTypeEnum.UPDATE.getStatus());
            setDelLogDefault(o, swStOperateLogDTO);
            swStOperateLogDTO.setAlterPersonId(SecurityUtils.getUser().getId());
            swStOperateLogDTO.setAlterTime(LocalDateTime.now());
            swStOperateLogDTO.setAlterField(filedName);
            swStOperateLogDTO.setFieldContent(Before);
            swStOperateLogDTO.setAlterFieldContent(After);
            swStOperateLogDTO.setChecker(SecurityUtils.getUser().getUsername());
            list.add(swStOperateLogDTO);
        }
        return list;
    }

    /**
     * 插入删除日志
     *
     * @param o
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static SwStOperateLogDTO setDelLog(Object o) {
        SwStOperateLogDTO swStOperateLogDTO = new SwStOperateLogDTO();
        setDelLogDefault(o, swStOperateLogDTO);
        swStOperateLogDTO.setLogType(LogTypeEnum.DEL.getStatus());
        swStOperateLogDTO.setAlterPersonId(SecurityUtils.getUser().getId());
        swStOperateLogDTO.setAlterTime(LocalDateTime.now());
        swStOperateLogDTO.setDeptId(SecurityUtils.getUser().getDeptId());
        return swStOperateLogDTO;
    }

    /**
     * 插入default值
     *
     * @param o
     * @param swStOperateLogDTO
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void setDelLogDefault(Object o, SwStOperateLogDTO swStOperateLogDTO) {
        try {
            Field dataCreatorId = o.getClass().getDeclaredField("creatorId");
            dataCreatorId.setAccessible(true);
            Object creatorId = dataCreatorId.get(o);
            Field dataCreatorName = o.getClass().getDeclaredField("creatorName");
            dataCreatorName.setAccessible(true);
            Object creatorName = dataCreatorName.get(o);
            Field dataCreateTime = o.getClass().getDeclaredField("createTime");
            dataCreateTime.setAccessible(true);
            Object createTime = dataCreateTime.get(o);
            swStOperateLogDTO.setDataCreatorId(creatorId.toString());
            swStOperateLogDTO.setDataCreatorName(creatorName.toString());
            swStOperateLogDTO.setDataCreateTime((LocalDateTime) createTime);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }

}
